package org.vectrics.common.db;

// J2EE Stuff
//import javax.ejb.EJBException;
import javax.naming.NamingException;

// Standard Objects
import org.apache.log4j.Category;
import org.vectrics.SystemException;

import javax.naming.InitialContext;

// Application Objects

//import org.vectrics.location.interfaces.*;
//import org.vectrics.user.interfaces.*;
//import org.vectrics.company.interfaces.*;

// For SQL Access
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;




public class DbConnection  {
    private static Category log = Category.getInstance(DbConnection.class);
    private Connection fConnection = null;
    private DataSource fDatasource   = null;
    private String fDataSourceName = null;
  
 
  
    public DbConnection(String dataSourceName) {
        fDataSourceName = dataSourceName;
    }
  
  /*
    public ResultSet executeSqlSelect(String queryStr) {
        if (log.isDebugEnabled()) {
            log.debug("executeSqlSelect() - called");
            log.debug("executeSqlSelect() - query = \r\n" + queryStr);
        }
        
        ResultSet result = null;
        Statement stmt = null;
        Connection connection = null; 
        try {
            connection = getDBConnection(); 
            stmt = connection.createStatement();
            result = stmt.executeQuery(queryStr);
        }
        catch (SQLException ae) {
            log.error("executeSqlSelect()", ae);
            throw new EJBException("SQLException while executing sql: "
                    + queryStr);
        }
        
        return(result);
    }
    */
    
    
    public Statement createStatement() {
        Statement statement = null;
        try {
            statement = getDBConnection().createStatement();
        }
        catch (Exception ex) {
            log.error("createStatment() - error", ex);
        }
        return(statement);
    }
    

    private void getDataSources() {
        try {
            InitialContext ic = new InitialContext();
            fDatasource = (DataSource)ic.lookup(fDataSourceName);
            if (fDatasource == null)
                throw new SystemException("Could not find data source for: "
                        + fDataSourceName);
        } catch (NamingException ne) {
            log.error("Exception getting datasource: " + fDataSourceName, ne);
            throw new SystemException("NamingException while looking " +
                            "up DB context  : " + ne.getMessage());
        }
    }



    public Connection getDBConnection() {
        try {
            log.debug("getDBConnection() - Getting db connection...");
            if (fDatasource == null)
                getDataSources();

            fConnection = fDatasource.getConnection();
        } catch (SQLException se) {
            log.error("getDBConnection()", se);
            throw new SystemException("SQL Exception while getting " +
                                "DB connection : \n" + se);
        }
        if (fConnection == null)
            throw new SystemException("Could not get db connection.");
        return(fConnection);
    }




    public void closeConnection() {
        try {
            if (fConnection != null && !fConnection.isClosed()) {
                fConnection.close();
            }
        } 
        catch (SQLException se) {
            log.error(se.getMessage());
            throw new SystemException("SQL Exception while closing " +
                                        "DB connection : \n" + se);
        }
    }


    public void closeResultSet(ResultSet result) throws SystemException {
        try {
            if (result != null) {
                result.close();
            }
        } catch (SQLException se) {
            throw new SystemException("SQL Exception while closing " +
                                        "Result Set : \n" + se);
        }
    }

    //------------------------------------------------------------------------------------
    public void closeStatement(Statement stmt) throws SystemException {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (SQLException se) {
            throw new SystemException("SQL Exception while closing " +
                                        "Statement : \n" + se);
        }
    }

    

   
    
}
